package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Role.Role;
import com.groupseven.backend.mapper.AuthoritiesMapper;
import com.groupseven.backend.mapper.MenuMapper;
import com.groupseven.backend.mapper.RoleMapper;
import com.groupseven.backend.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AuthoritiesMapper authoritiesMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * @Author 王志华
     * @return
     */
    public List<Role> getAllRoles(){
        return roleMapper.getAllRoles();
    }

    /**
     * @Author 王志华
     * @param Username
     * @param Rolenumber
     * @return
     */
    public int updateRoleByUsernameAndRolenumber(String Username,int Rolenumber){
        return roleMapper.updateRoleByUsernameAndRolenumber(Username,Rolenumber);
    }

    /**
     * @Author 王志华
     * @param role
     * @return
     */
    public int addRole(Role role){
        return roleMapper.addRole(role);
    }

    /**
     * @Author 王志华
     * @param Rolenumber
     * @return
     */
    public int deleteRoleByRolenumber(int Rolenumber){
        return roleMapper.deleteRoleByRolenumber(Rolenumber);
    }

    /**
     * @Author 王志华
     * @param role
     * @return
     */
    public int updateRole(Role role){
        return roleMapper.updateRole(role);
    }

    /**
     * @Author 王志华
     * @param query
     * @return
     */
    public List<Role> FuzzySearch(String query){
        query = "%" + query + "%";
        return roleMapper.FuzzySearch(query);
    }

    /**
     *
     * @param roles
     * @return
     */
    public boolean multi_delete(List<Role> roles){
        List<Integer> deleteList = new ArrayList<>();
        for(Role iterator : roles){
            deleteList.add(iterator.getRolenumber());
        }
        Map<String,Object> query = new HashMap<>();
        query.put("id",deleteList);
        if(roleMapper.multi_delete(query)==0||authoritiesMapper.multi_deleteByRolenumber(query)==0||menuMapper.multi_deleteByRolenumber(query)==0){
            return false;
        }
        return true;
    }
}
